package com.transsion.clouddemo.core.Custom.utils

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.widget.Toast

object ToastUtils {

    private var mToast: Toast? = null
    private val mHandler = Handler(Looper.getMainLooper())
    private val run = Runnable {
        mToast?.cancel()
    }

    // 显示短时间的Toast
    fun showShort(context: Context, message: String) {
        mToast?.cancel()
        mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT).apply {
            show()
        }
    }

    // 显示长时间的Toast
    fun showLong(context: Context, message: String) {
        mToast?.cancel()
        mToast = Toast.makeText(context, message, Toast.LENGTH_LONG).apply {
            show()
        }
    }

    // 自定义时长的Toast
    fun showCustomTime(context: Context, message: String, duration: Long) {
        mToast?.cancel()
        val toast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
        mToast = toast.apply {
            show()
        }

        mHandler.postDelayed({
            mToast?.cancel()
        }, duration)
    }
}
